package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.AuthDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * APP实名认证信息表 服务实现类
 *
 * @author hyk
 */
@Slf4j
@Service
@Transactional
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    /**
     * 查询用户审核列表
     *
     * @param dto dto
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult list(AuthDto dto) {
        if (null == dto) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        IPage<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<ApUserRealname> lqw = new LambdaQueryWrapper<>();
        if (null != dto.getStatus()) {
            lqw.eq(ApUserRealname::getStatus, dto.getStatus());
        }
        page = page(page, lqw);
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    /**
     * 身份审核失败
     *
     * @param dto dto
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult authFail(AuthDto dto) {
        if (null == dto) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        update(Wrappers.<ApUserRealname>lambdaUpdate()
                .eq(ApUserRealname::getId, dto.getId())
                .set(ApUserRealname::getStatus, 2)
                .set(ApUserRealname::getReason, dto.getMsg())
        );
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 身份审核成功
     *
     * @param dto dto
     * @return {@link ResponseResult}
     */
    @Override
    public ResponseResult authPass(AuthDto dto) {
        if (null == dto) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        /*update(Wrappers.<ApUserRealname>lambdaUpdate()
                .eq(ApUserRealname::getId, dto.getId())
                .set(ApUserRealname::getStatus, 9)
        );*/
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
